package com.ls.fw.mvc.springmvc.support.view.template.beetl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ls.fw.commons.config.property.FwPropertyPlaceholderConfigurer;
import com.ls.fw.commons.config.utils.SpringPropertyResourceReader;
import com.ls.fw.commons.core.utils.collections.CollectionUtils;
import com.ls.fw.commons.core.utils.collections.MapUtils.CollectionMapper;
import com.ls.fw.commons.spring.SpringContextHolder;

public class FwBeetlGroupUtilConfiguration extends BeetlGroupUtilConfiguration{

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	protected Set<String> viewPaths = null;
	
	protected FwPropertyPlaceholderConfigurer propertyConfigurer = null;
	
	@Override
	public void setServletContext(ServletContext sc)
	{
		super.setServletContext(sc);
		if(this.sharedVars ==null){
			sharedVars = new HashMap<String, Object>();
		}
		String ctx = sc.getContextPath();
		sharedVars.put("ctx", ctx);
		if(viewPaths != null && sharedVars != null){
			Iterator<String> it= viewPaths.iterator();
			String key = null;           
			while(it.hasNext()){    
				key = it.next();           
		        String v = (String) sharedVars.get(key);
		        sharedVars.put(key, ctx+v);
			}   
		}
		try {
			Map<String, String> map = SpringPropertyResourceReader.getPropsMap();
			if(map != null){
				sharedVars.putAll(map);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		this.logger.info("共享变量：{}",this.sharedVars);
		this.logger.info("页面绝对路径：{}",this.viewPaths);
		this.load(sc);
	}
	private void load(final ServletContext servletContext){
		List<FwPropertyPlaceholderConfigurer> configurers = SpringContextHolder.findBeansByType(FwPropertyPlaceholderConfigurer.class);
		if(configurers!=null){
			for (FwPropertyPlaceholderConfigurer fwPropertyPlaceholderConfigurer : configurers) {
				Map<String, String> map = fwPropertyPlaceholderConfigurer.getPropsMap();
				if(map != null){
					CollectionUtils.traversal(map, new CollectionMapper() {
						
						@Override
						public boolean next(Object key, Object value) {
							servletContext.setAttribute(String.valueOf(key).replace(".", "_"), String.valueOf(value));
							return true;
						}
					});
				}
			}
		}
	}
	@Override
	public void setSharedVars(Map<String, Object> sharedVars)
	{
		if(this.sharedVars !=null){
			if(sharedVars!=null){
				this.sharedVars.putAll(sharedVars);
			}
		}else{
			this.sharedVars = sharedVars;
		}
	}

	public Set<String> getViewPaths() {
		return viewPaths;
	}

	public void setViewPaths(Set<String> viewPaths) {
		this.viewPaths = viewPaths;
	}

	public FwPropertyPlaceholderConfigurer getPropertyConfigurer() {
		return this.propertyConfigurer;
	}

	public void setPropertyConfigurer(
			FwPropertyPlaceholderConfigurer propertyConfigurer) {
		this.propertyConfigurer = propertyConfigurer;
	}

}
